<?php

/**
 * This is the model class for table "{{cmp_punish}}".
 *
 * The followings are the available columns in table '{{cmp_punish}}':
 * @property string $zcu_id
 * @property integer $zm_id
 * @property string $zcu_reason
 * @property string $zcu_result
 * @property double $zcu_integral
 * @property integer $zam_id
 * @property integer $inputtime
 */
class CmpPunish extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return CmpPunish the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{cmp_punish}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
		//	array('zcu_id', 'required'),
			array('zm_id, zam_id, inputtime, zcu_del', 'numerical', 'integerOnly'=>true),
			array('zcu_integral', 'numerical'),
			array('zcu_id', 'length', 'max'=>20),
			array('zcu_reason, zcu_result', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zcu_id, zm_id, zcu_reason, zcu_result, zcu_del, zcu_integral, zam_id, inputtime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'company'=>array(self::BELONGS_TO, 'Company', 'zm_id'),
				'admin'=>array(self::BELONGS_TO, 'Adminer', 'zam_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zcu_id' => 'Zcu',
			'zm_id' => 'Zm',
			'zcu_reason' => 'Zcu Reason',
			'zcu_result' => 'Zcu Result',
			'zcu_integral' => 'Zcu Integral',
			'zam_id' => 'Zam',
			'inputtime' => 'Inputtime',
			'zcu_del' => 'zcu_del',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zcu_id',$this->zcu_id,true);
		$criteria->compare('zm_id',$this->zm_id);
		$criteria->compare('zcu_reason',$this->zcu_reason,true);
		$criteria->compare('zcu_result',$this->zcu_result,true);
		$criteria->compare('zcu_integral',$this->zcu_integral);
		$criteria->compare('zam_id',$this->zam_id);
		$criteria->compare('zcu_del',$this->zcu_del);
		$criteria->compare('inputtime',$this->inputtime);
		
		$criteria->order = 'inputtime DESC';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	protected function beforeSave(){
		if($this->isNewRecord){
			$this->zcu_id = $this->createRandNo();
			$this->zam_id = Yii::app()->user->getId();
			$this->zcu_del = 1;
			$this->inputtime = time();
		}
		return true;
	}
	
	
	/**
	 * 生成违规编号
	 */
	public function  createRandNo(){
		$keyid = substr(time(),-8).rand(111,999);
		if(!$this->findByPk($keyid)){
			return $keyid;
		}else{
			return $this->createRandNo();
		}
	}
	
	/**
	 * 获取违规类型
	 */
	public function getZcuReason($type=null){
		if($type==null){
			$punish = Setting::model()->findByAttributes(array('zkl_variable'=>'PunishReacon'));
			if(!empty($punish)){
				$punish = unserialize($punish->zkl_valued);
				return $punish;
			}
		}
	}
	
	/**
	 * 获取违规处罚
	 */
	public function getZcuResult($type=null){
		if($type==null){
			$punish = Setting::model()->findByAttributes(array('zkl_variable'=>'PunishResult'));
			if(!empty($punish)){
				$punish = unserialize($punish->zkl_valued);
				return $punish;
			}
		}
	}
	
	/**
	 * 获取违规总分
	 */
	public function getSumpoint($type=null){
		if($type==null){
			$punish = Setting::model()->findByAttributes(array('zkl_variable'=>'PunishSumpoint'));
			if(!empty($punish)){
				$punish = $punish->zkl_valued;
				return $punish;
			}
		}
	}
}